package com.ankicoo.file.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Objects;

/**
 * Description: 文件 配置
 *
 * @author : QCYANG
 * @date : 2021/7/15 16:55
 */
@Configuration
public class OssConnect {
    @Value("${OSS.endpoint}")
    private String endpoint;

    @Value("${OSS.accessKeyId}")
    private String accessKeyId;

    @Value("${OSS.accessKeySecret}")
    private String accessKeySecret;

    @Value("${OSS.bucketName}")
    public String bucketName;

    private OSS ossClient;

    @PostConstruct
    private void createOssClient() {
        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    @PreDestroy
    private void destroyOssClient(){
        if (Objects.nonNull(ossClient)){
            ossClient.shutdown();
        }
    }

    public OSS getOssClient(){
        return ossClient;
    }

}
